﻿@using PlatformNetCore.Entity
@{
    ViewBag.Title = "编辑文献";
}
@model News
<style>
    html {
        background-color: #fff;
    }

    .layui-card {
        box-shadow: none;
    }
</style>

<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form" action="@Url.Action("DocumentEdit","News",new { area = "Admin"})" lay-filter="useredit-form">
            <div class="layui-form-item">
                <label class="layui-form-label">标题*</label>
                <div class="layui-input-block">
                    <input type="hidden" name="ID" lay-verify="required" autocomplete="on" class="layui-input">
                    <input type="hidden" name="NewsType" value="@NewsType.Literature.ToString()" />
                    <input type="text" name="Title" lay-verify="required" placeholder="标题" autocomplete="on" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">上传文献</label>
                <div class="layui-input-block">
                    <div class="layui-upload-drag" id="transfer-upload">
                        <i class="layui-icon"></i>
                        <p>点击上传，或将PDF/Word文件拖拽到此处</p>
                    </div>
                    <span id="excelName" class="layui-inline">@Model.Content</span>
                    <input id="Content" name="Content" value="@Model.Content" type="hidden" class="layui-input" />
                    <input id="CoverImage" name="CoverImage" value="@Model.CoverImage" type="hidden" class="layui-input" />
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">来源</label>
                <div class="layui-input-block">
                    <input type="text" name="Source" placeholder="来源" maxlength="100" autocomplete="on" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">发布时间</label>
                <div class="layui-input-block">
                    <input type="text" id="publishDate" name="PublishDate" value='@(Model.PublishDate.HasValue ? Model.PublishDate.Value.ToString("yyyy-MM-dd") : "")' placeholder="发布时间" autocomplete="on" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">摘要</label>
                <div class="layui-input-block">
                    <textarea type="text" class="layui-textarea" name="Remark" placeholder="摘要"></textarea>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">排序号</label>
                <div class="layui-input-block">
                    <input type="text" name="SortNo" lay-verify="required" placeholder="排序号" autocomplete="on" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">是否推荐</label>
                <div class="layui-input-block">
                    <input type="checkbox" checked="" name="IsRecommend" value="1" lay-skin="switch" lay-filter="recommend-Switch" lay-text="是|否">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">是否显示</label>
                <div class="layui-input-block">
                    <input type="checkbox" checked="" name="IsAvailable" value="1" lay-skin="switch" lay-filter="available-Switch" lay-text="显示|隐藏">
                </div>
            </div>
            <div id="useredit-submit" lay-submit lay-filter="useredit-submit"></div>
        </form>
    </div>
</div>
<script>
    layui.use(['laydate','form','upload'], function () {
        var $ = layui.$, form = layui.form;
        var laydate = layui.laydate;
        var upload = layui.upload;

        form.render(null, 'useredit-form');

        //执行一个laydate实例
        laydate.render({
            elem: '#publishDate' //指定元素
        });

        /* 监听指定开关 */
        form.on('switch(recommend-Switch)', function (data) {
            $(this).val(this.checked ? '1' : '0')
        });
        form.on('switch(available-Switch)', function (data) {
            $(this).val(this.checked ? '1' : '0')
        });

        //从父层获取值，json是父层的全局js变量。eval是将该string类型的json串变为标准的json串
        var editObj = parent.editData;
        //表单初始赋值
        form.val('useredit-form', {
            "ID": editObj.ID
            , "Title": editObj.Title
            , "NewsType": editObj.NewsType
            , "IsRecommend": editObj.IsRecommend
            , "IsAvailable": editObj.IsAvailable
            , "Source": editObj.Source
            , "Remark": editObj.Remark
            , "CoverImage": editObj.CoverImage
            , "SortNo": editObj.SortNo
            , "Content": editObj.Content
        });

        var iframeId = $(window.frameElement).attr("id");
        //设定文件大小限制
        var loadingFlag;
        upload.render({
            elem: '#transfer-upload'
            , url: '/Admin/File/Upload'
            , size: 10240 //限制文件大小 10M，单位 KB
            ,accept: 'file' //普通文件
            , exts: 'pdf|doc|docx' //只允许上传Excel文件
            , data: { UploadFileType: "@(PlatformNetCore.Entity.UploadFileType.ProductDetailImage.ToString())" }
            , before: function (obj) {
                $(parent.document).find("#" + iframeId).parent().nextAll("div").find("a[class='layui-layer-btn0']").attr("disabled", true).css("pointer-events", "none");
                loadingFlag = layer.msg('正在加载，请稍候……', { icon: 16, shade: 0.01, shadeClose: false, time: 10000 });
                //预读本地文件示例，不支持ie8
                obj.preview(function (index, file, result) {
                    $('#excelName').text(file.name); //图片链接（base64）
                    $("#Content").val(file.name);
                });
            }
            , done: function (res) {
                layer.close(loadingFlag);
                $(parent.document).find("#" + iframeId).parent().nextAll("div").find("a[class='layui-layer-btn0']").removeAttr("disabled").removeAttr("style");
                if (res.success) {
                    //提交 Ajax 成功后
                    $("#CoverImage").val(res.data);
                }
                else {
                    layer.msg(res.msg, { icon: 2, time: 3000 });
                }
            }
            ,error: function() {
                layer.msg('上传失败，请重试', { icon: 2, time: 1500 });
            }
        });

    });
</script>
